package com.ybdz.scanner.activity;

import android.Manifest;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.Nullable;
import android.support.v4.app.ActivityCompat;
import android.telephony.TelephonyManager;
import android.widget.Button;
import android.widget.EditText;
import com.ybdz.scanner.R;
import com.ybdz.scanner.api.ApiStore;
import com.ybdz.scanner.base.BaseActivity;
import com.ybdz.scanner.bean.LoginBean;
import com.ybdz.scanner.utils.MyApplication;
import com.ybdz.scanner.utils.MyToast;
import com.ybdz.scanner.utils.SPUtils;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * @ClassName: LoginActivity
 * @author: zhao
 * @Description: 登录界面
 * @date 2018/2/24 9:34
 */
public class LoginActivity extends BaseActivity {

    @BindView(R.id.usename)
    EditText mUsename;
    @BindView(R.id.password)
    EditText mPassword;
    @BindView(R.id.login)
    Button mLogin;


    private int REQUEST_PHONE_STATE = 0;
    private static String IEMI;    //手机唯一标识
    private String mName;
    private String mPass;
    private Handler mHandler;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        ButterKnife.bind(this);
        IEMI = getIMEI();
        SPUtils.saveString(this, "IEMI", IEMI);
    }

    private String getIMEI() {
        //Android6.0需要动态获取权限
        if (ActivityCompat.checkSelfPermission(this, Manifest.permission.READ_PHONE_STATE) != PackageManager.PERMISSION_GRANTED) {
            //toast("需要动态获取权限");
            ActivityCompat.requestPermissions(LoginActivity.this, new String[]{Manifest.permission.READ_PHONE_STATE}, REQUEST_PHONE_STATE);
        } else {
            //toast("不需要动态获取权限");
            TelephonyManager TelephonyMgr = (TelephonyManager) getSystemService(TELEPHONY_SERVICE);
            IEMI = TelephonyMgr.getDeviceId();
        }
        return IEMI;
    }

    @OnClick(R.id.login)
    public void onViewClicked() {
        mName = mUsename.getText().toString().trim();
        mPass = mPassword.getText().toString().trim();
        if (mName.length() <= 0) {
            MyToast.show(LoginActivity.this, "账号不能为空");
        } else if (mPass.length() <= 0) {
            MyToast.show(LoginActivity.this, "密码不能为空");
        } else {
            getTicket();
        }

    }

    /**
     * 获取用户输入的账号密码，请求登录
     */
    private void getTicket() {
       showLoading();
        ApiStore.login(mName,mPass,IEMI).enqueue(new Callback<LoginBean>() {
            @Override
            public void onResponse(Call<LoginBean> call, Response<LoginBean> response) {
                if (response.isSuccessful()) {
                    saveLogin();
                    hideLoading();
                } else {
                    hideLoading();
                    onFail();
                }
            }

            @Override
            public void onFailure(Call<LoginBean> call, Throwable t) {
                hideLoading();
                onFail();
            }
        });
    }

    /**
     * 保存登录信息到SharedPreferences
     */
    private void saveLogin() {
        SPUtils.saveString(LoginActivity.this, "usename", mName);
        MyApplication.setIsLogin(true);
        MyToast.show(LoginActivity.this, "登录成功");
        startActivity(new Intent(LoginActivity.this, MainActivity.class));
        LoginActivity.this.finish();
    }
}
